package com.example.treehole.ui.page.login

import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

class VerifyCodeViewModel: ViewModel(){
    val isSendingCode = mutableStateOf(false)
    val countDownTime = mutableIntStateOf(0)

    private var countDownJob:Job? = null

    fun startCountDown(){
        if(!isSendingCode.value){
            isSendingCode.value = true
            countDownTime.intValue = 60
            countDownJob = viewModelScope.launch {
                while (countDownTime.intValue>0){
                    delay(1000)
                    countDownTime.intValue--
                }
                isSendingCode.value = false
            }
        }
    }

    fun resetCountDown(){
        countDownJob?.cancel()
        isSendingCode.value = false
        countDownTime.intValue = 0
    }
}